package com.hualan.servlet;


import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.bean.PanghuDeliveryOrder;
import com.hualan.dao.PanghuDeliveryOrderDao;
import com.hualan.dao.impl.PanghuDeliveryOrderDaoImpl;
import com.hualan.enums.ResponseEnum;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@WebServlet("/delivery/order")
public class DeliveryOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取配送订单类型
        String deliveryOrderTypeString = req.getParameter("deliveryOrderType");
        int deliveryOrderType = deliveryOrderTypeString == null ? -1 : Integer.parseInt(deliveryOrderTypeString);


        //2.获取前端传递的分页信息   page 和 size
        String pageString = req.getParameter("page");
        int page = pageString == null ? 1 : Integer.parseInt(pageString);
        System.out.println("page = " + page);


        String sizeString = req.getParameter("size");
        int size = sizeString == null ? 10 : Integer.parseInt(sizeString);
        System.out.println("size = " + size);

        //3.获取前端传递订单状态

        String orderStatusString = req.getParameter("orderStatus");
        int orderStatus = Objects.equals(orderStatusString, "") ? 0 : Integer.parseInt(orderStatusString);
        System.out.println("orderStatus = " + orderStatus);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //4.订单创建时间,订单完成时间
        String orderCreateTimeString = req.getParameter("orderCreateTime");
        orderCreateTimeString = "".equals(orderCreateTimeString) ? "1970-01-02 08:00:00" : orderCreateTimeString;
        System.out.println("orderCreateTimeString = " + orderCreateTimeString);

        Date orderCreateTime = null;
        try {
            orderCreateTime = sdf.parse(orderCreateTimeString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println("orderCreateTime = " + orderCreateTime);

        String orderDeliveryTimeString = req.getParameter("orderDeliveryTime");
        orderDeliveryTimeString = "".equals(orderDeliveryTimeString) ? "2098-01-02 08:00:00" : orderDeliveryTimeString;
        Date orderDeliveryTime = null;
        try {
            orderDeliveryTime = sdf.parse(orderDeliveryTimeString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        System.out.println("orderDeliveryTime = " + orderDeliveryTime);

        //5.收货人手机号
        String deliveryUserPhoneString = req.getParameter("deliveryUserPhone");
        String deliveryUserPhone = deliveryUserPhoneString == null ? "" : deliveryUserPhoneString;
        System.out.println("deliveryUserPhone = " + deliveryUserPhone);

        //6.收货联系人名字
        String deliverUserNameString = req.getParameter("deliveryUserName");
        String deliverUserName = deliverUserNameString == null ? "" : deliverUserNameString;
        System.out.println("deliverUserName = " + deliverUserName);


        //7.创建Map对象 用来给前端返回信息
        HashMap<String, Object> map = new HashMap<>();

        PanghuDeliveryOrderDao panghuDeliveryOrderDao = new PanghuDeliveryOrderDaoImpl();

//        //8.查询数据总条数---增删改查来自Dao
        Long count = panghuDeliveryOrderDao.selectAllPanghuDeliveryOrderCount(deliveryOrderType, orderStatus, deliveryUserPhone, deliverUserName, orderCreateTime, orderDeliveryTime);

//        //按数量存储到map中
        map.put("total", count);


        //9.按照条件查询数据
        List<PanghuDeliveryOrder> orderList = panghuDeliveryOrderDao.getPanghuDeliveryOrder(deliveryOrderType, orderStatus, deliveryUserPhone, deliverUserName, orderCreateTime, orderDeliveryTime, page, size);

        //将数据以键值对 存储到map中
        map.put("list", orderList);


        //10.设置code状态码 200成功 300失败
        if (orderList != null) {
//            map.put("code", 200);
//            map.put("message", "请求成功");
            resp.getWriter().write(JSON.toJSONString(JsonResult.success(map)));

        } else {
            resp.getWriter().write(JSON.toJSONString(JsonResult.error(ResponseEnum.INCORRECT_PARAMS, "请求失败")));
        }

        //11.将数据源map  通过response返回给前端
        //问题?
        //.json是文件存储数据的一种格式!参考 .txt .word .png等仅仅是一种文件格式
        //但是其内部遵循键值对{}和数组[]结构;所以我们在进行网络数据输出都会使用此格式的数据进行传递!!!
//        String jsonString =
//        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
